<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>
    <script>
        /* 
        CORS(Cross-Origin Resource Sharing, 跨源资源共享)是W3C出的一个标准,想要实现跨域主要靠服务器进行一些设置。客户端不用做任何更改！
        Access-Control-Allow-Origin 允许的域
        Access-Control-Allow-Credentials 允许携带cookie
        Access-Control-Expose-Headers 允许客户端获取哪个头
        Access-Control-Allow-Methods 允许的方法
        Access-Control-Allow-Headers 允许哪些头
        Access-Control-Max-Age 预检请求的结果缓存
        cors就是设置各种约定好的头 首选该方式
        */
        let xhr = new XMLHttpRequest;
        document.cookie = 'name=zfpx';
        xhr.withCredentials = true;
        xhr.open('PUT', 'http://localhost:4000/getData', true);
        xhr.setRequestHeader('name', 'zfpx');
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                    console.log(xhr.response);
                    console.log(xhr.getResponseHeader('name'));
                }
            }
        }
        xhr.send();
    </script>
    
</body>

</html>